package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacHuaLongUser;
import com.csun.cmny.provider.model.domain.UacUser;
import com.csun.cmny.provider.model.dto.hualong.HuaLongDoctorAuditDto;
import com.csun.cmny.provider.model.vo.hualong.*;
import com.csun.cmny.provider.service.UacHuaLongUserService;
import com.csun.cmny.provider.utils.HttpClient;
import com.csun.cmny.provider.utils.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.HuaLongService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-10-23 17:42
 * @Description:
 */
@RestController
@RequestMapping(value = "/hualong", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class HuaLongController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private HuaLongService huaLongService;
    @Autowired
    private UacHuaLongUserService uacHuaLongUserService;
    @Autowired
    HttpClient httpClient;

    /**
     * 登录测试 - 不正式使用
     *
     * @param acount
     * @param password
     * @param role
     * @return
     */
    @PostMapping(value = "/login")
    public Wrapper login(String acount, String password, String role) {

        UacUser uacUser = huaLongService.login(acount, password, role);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), uacUser);
    }

    @PostMapping(value = "/getHuaLongId")
    public Wrapper getHuaLongId() {

        Long userId = getLoginAuthDto().getUserId();

        UacHuaLongUser huaLongUser = uacHuaLongUserService.selectByKey(userId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), huaLongUser);
    }

    /**
     * 医生列表
     *
     * @param page  页数 (默认为1)
     * @param size  每页显示条数（默认为10）
     *
     * @return  the list hua long doctor vo
     */
    @PostMapping(value = "/doctorList")
    public Wrapper doctorList(Integer page, Integer size) {

//        List<HuaLongDoctorVo> huaLongDoctorVoList = huaLongService.getHuaLongDoctorList(page, size);

        Object object = huaLongService.getHuaLongDoctorList(page, size);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);

    }

    /**
     * 医生详情
     *
     * @param id    the id
     *
     * @return  the hua long doctor vo
     */
    @PostMapping(value = "/doctorInfo/{id}")
    public Wrapper doctorInfo(@PathVariable Integer id) {

//        HuaLongDoctorVo huaLongDoctorVo = huaLongService.getHuaLongDoctorById(id);
        Object object = huaLongService.getHuaLongDoctorById(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 主管列表
     *
     * @param page  页数 (默认为1)
     * @param size  每页显示条数（默认为10）
     *
     * @return  the list hua long super vo
     */
    @PostMapping(value = "/superList")
    public Wrapper superList(Integer page, Integer size) {

//        List<HuaLongSuperVo> huaLongSuperVoList = huaLongService.getHuaLongSuperList(page, size);

        Object object = huaLongService.getHuaLongSuperList(page, size);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);

    }

    /**
     * 主管详情
     *
     * @param id    the id
     *
     * @return  the hua long super vo
     */
    @PostMapping(value = "/superInfo/{id}")
    public Wrapper superInfo(@PathVariable Integer id) {

//        HuaLongSuperVo huaLongSuperVo = huaLongService.getHuaLongSuperById(id);
        Object object = huaLongService.getHuaLongSuperById(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 护工列表
     *
     * @param page  页数 (默认为1)
     * @param size  每页显示条数（默认为10）
     *
     * @return  the list hua long nurse vo
     */
    @PostMapping(value = "/nurseList")
    public Wrapper nurseList(Integer page, Integer size) {

//        List<HuaLongNurseVo> huaLongNurseVoList = huaLongService.getHuaLongNurseList(page, size);

        Object object = huaLongService.getHuaLongNurseList(page, size);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);

    }

    /**
     * 护工详情
     *
     * @param id    the id
     *
     * @return  the hua long nurse vo
     */
    @PostMapping(value = "/nurseInfo/{id}")
    public Wrapper nurseInfo(@PathVariable Integer id) {

//        HuaLongNurseVo huaLongNurseVo = huaLongService.getHuaLongNurseById(id);
        Object object = huaLongService.getHuaLongNurseById(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 护工获取对应楼栋
     *
     * @param id    the id
     *
     * @return  the hua long nurse vo
     */
    @PostMapping(value = "/nurseBuildInfo/{id}")
    public Wrapper nurseBuildInfo(@PathVariable Integer id) {

        Object object = huaLongService.getHuaLongNurseBuildInfoId(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 班组列表
     *
     * @param page  页数 (默认为1)
     * @param size  每页显示条数（默认为10）
     *
     * @return  the list hua long team vo
     */
    @PostMapping(value = "/teamList")
    public Wrapper teamList(Integer page, Integer size) {

//        List<HuaLongTeamVo> huaLongTeamVoList = huaLongService.getHuaLongTeamList(page, size);
        Object object = huaLongService.getHuaLongTeamList(page, size);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);

    }

    /**
     * 班组详情
     *
     * @param id    the id
     *
     * @return  the hua long team vo
     */
    @PostMapping(value = "/teamInfo/{id}")
    public Wrapper teamInfo(@PathVariable Integer id) {

//        HuaLongTeamVo huaLongTeamVo = huaLongService.getHuaLongTeamById(id);
        Object object = huaLongService.getHuaLongTeamById(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 长者列表
     *
     * @param page  页数 (默认为1)
     * @param size  每页显示条数（默认为10）
     *
     * @return  the list hua long elder vo
     */
    @PostMapping(value = "/elderList")
    public Wrapper elderList(Integer page, Integer size) {

//        List<HuaLongElderVo> huaLongElderVoList = huaLongService.getHuaLongElderList(page, size);

        Object object = huaLongService.getHuaLongElderList(page, size);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);

    }

    /**
     * 长者详情
     *
     * @param id    the id
     *
     * @return  the hua long elder vo
     */
    @PostMapping(value = "/elderInfo/{id}")
    public Wrapper elderInfo(@PathVariable Integer id) {

        Object object = huaLongService.getHuaLongElderId(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 获取楼栋房间
     *
     * @return
     */
    @PostMapping(value = "/buildList")
    public Wrapper buildList() {

//        HuaLongBuildInfoVo huaLongBuildInfoVo = huaLongService.getHuaLongBuildList();

        Object object = huaLongService.getHuaLongBuildList();

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 获取楼栋房间
     *
     * @return
     */
    @PostMapping(value = "/roomBedInfo")
    public Wrapper roomBedInfo(Integer roomId) {

//        HuaLongRoomInfoVo huaLongRoomBedInfo = huaLongService.getHuaLongRoomBedInfo(roomId);
        Object object = huaLongService.getHuaLongRoomBedInfo(roomId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 医生审核预约信息
     *
     * @param huaLongDoctorAuditDto the hua long doctor audit dto
     *
     * @return
     */
    @PostMapping(value = "/doctorAudit", consumes = "multipart/form-data")
    public Wrapper doctorAudit(HttpServletRequest request, HuaLongDoctorAuditDto huaLongDoctorAuditDto) throws IOException {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> fileList = multipartRequest.getFiles("file");
        if (PublicUtil.isNotEmpty(fileList)) {
//            huaLongDoctorAuditDto.setSignature(fileList.get(0));
            File f = File.createTempFile("tmp", null);
            fileList.get(0).transferTo(f);
            huaLongDoctorAuditDto.setSignature(f);

            f.deleteOnExit();
        }

        Object object = huaLongService.huaLongDoctorAuditElder(huaLongDoctorAuditDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 医生可查看预约列表
     *
     * @param page  页数 (默认为1)
     * @param size  每页显示条数（默认为10）
     *
     * @return
     */
    @PostMapping(value = "/appointmentList")
    public Wrapper appointmentList(Integer page, Integer size) {

//        List<HuaLongAppointmentVo> huaLongAppointmentVoList = huaLongService.getHuaLongAppointmentList(page, size);

        Object object = huaLongService.getHuaLongAppointmentList(page, size);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 医生查看预约详情
     *
     * @param id    预约id
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/appointmentInfo/{id}")
    public Wrapper appointmentInfo(@PathVariable Integer id) {

        Object object = huaLongService.getHuaLongAppointmentInfo(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 医生端（修改密码）
     *
     * @param id            医生id
     * @param password      输入密码
     * @param repassword    确认密码
     *
     * @return
     */
    @PostMapping(value = "/doctor/modifyPwd")
    public Wrapper doctorModifyPwd(Integer id, String password, String repassword) {

        Object object = huaLongService.doctorModifyPwd(id, password, repassword);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

    /**
     * 护士端（修改密码）
     *
     * @param id            医生id
     * @param password      输入密码
     * @param repassword    确认密码
     *
     * @return
     */
    @PostMapping(value = "/nurse/modifyPwd")
    public Wrapper nurseModifyPwd(Integer id, String password, String repassword) {

        Object object = huaLongService.nurseModifyPwd(id, password, repassword);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), object);
    }

}
